#include "Controls.h"

Controls::Controls(Config* config, Camera* camera, Graphics* graphics) {
	this->config = config;
	this->camera = camera;
    this->graphics = graphics;

	goingLeft = false;
	goingRight = false;
	goingForward = false;
	goingBack = false;
	goingUp = false;
	goingDown = false;
	sprinting = false;
}

void Controls::keyDown(int key) {
	if (key == config->MoveCamForward)		goingForward = true;
	else if (key == config->MoveCamBack)	goingBack = true;
	else if (key == config->MoveCamLeft)	goingLeft = true;
	else if (key == config->MoveCamRight)	goingRight = true;
	else if (key == config->MoveCamUp)		goingUp = true;
	else if (key == config->MoveCamDown)	goingDown = true;

	else if (key == config->btnChangeRenderMode)
		config->ToggleRenderMode();
	else if (key == config->btnResetCamera)
		camera->ResetCamera();
	else if (key == config->btnToggleFlyCamera)
		config->ToggleFlyCamera();
	else if (key == config->btnToggleSprint)
		sprinting = true;
	else if (key == config->btnTargetFpsIncrease)
		config->IncreaseTargetFPS();
	else if (key == config->btnTargetFpsDecrease)
		config->DecreaseTargetFPS();
	else if (key == config->btnToggleFpsCap)
		config->ToggleFrameCapped();
	else if (key == config->btnToggleDisplayStats)
		config->ToggleDisplayStats();
}

void Controls::keyUp(int key) {//dd
	if (key == config->MoveCamForward)		goingForward = false;
	else if (key == config->MoveCamBack)	goingBack = false;
	else if (key == config->MoveCamLeft)	goingLeft = false;
	else if (key == config->MoveCamRight)	goingRight = false;
	else if (key == config->MoveCamUp)		goingUp = false;
	else if (key == config->MoveCamDown)	goingDown = false;

	else if (key == config->btnToggleSprint)
		sprinting = false;
}

void Controls::Move(double fps) {
	double speed;
	speed = (sprinting) ? config->CameraMovementSpeedSprint : config->CameraMovementSpeed;
	if (goingLeft)		camera->Strafe((float)-speed * (1.0f/(float)fps));
	if (goingRight)		camera->Strafe((float)speed * (1.0f / (float)fps));
	if (goingForward)	camera->Walk((float)speed * (1.0f / (float)fps));
	if (goingBack)		camera->Walk((float)-speed * (1.0f / (float)fps));
	if (goingUp)		camera->Hover((float)speed * (1.0f / (float)fps));
	if (goingDown)		camera->Hover((float)-speed * (1.0f / (float)fps));
}

static int mLastMousePosX = 0,
           mLastMousePosY = 0;

void Controls::MouseMovement(WPARAM btnState, int x, int y) {
    if((btnState & MK_LBUTTON) != 0) { //if the left mouse button is pressed
        // Make each pixel correspond to a quarter of a degree. 
		float dx = (FLOAT)XMConvertToRadians(config->CameraRotationSpeed * static_cast < float >(x - mLastMousePosX));
		float dy = (FLOAT)XMConvertToRadians(config->CameraRotationSpeed * static_cast < float >(y - mLastMousePosY));

		//float dx = XMConvertToRadians(config->CameraRotationSpeed * static_cast < float >( x - mLastMousePosX));
		//float dy = XMConvertToRadians(config->CameraRotationSpeed * static_cast < float >(y - mLastMousePosY));
        camera->Pitch(dy);
        camera->RotateY(dx); // I inverted horizontal mouse movement
    }
    mLastMousePosX = x; 
    mLastMousePosY = y;
}
